import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _slice from "lodash/slice";
import _map from "lodash/map";
import _omit from "lodash/omit";
var _excluded = ["tplFrom", "tplTo"];
import React from 'react';
import { Tabs } from 'antd';
import { dynamicTabsOmitProps } from "../config";
import { get } from "../utils";

var DynamicTabs = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicTabs, _React$Component);

  var _super = _createSuper(DynamicTabs);

  function DynamicTabs() {
    var _this;

    _classCallCheck(this, DynamicTabs);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.ref = {};
    return _this;
  }

  _createClass(DynamicTabs, [{
    key: "render",
    value: function render() {
      var panes = this.renderPanes();

      var otherProps = _omit(this.props, dynamicTabsOmitProps);

      return /*#__PURE__*/React.createElement(Tabs, Object.assign({}, otherProps, {
        animated: false
      }), panes);
    }
  }, {
    key: "renderPanes",
    value: function renderPanes() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$children = _this$props.children,
          children = _this$props$children === void 0 ? [] : _this$props$children,
          fields = _this$props.fields,
          context = _this$props.context;

      var tabPanes = _map(fields, function (field) {
        var _field$props = field.props;
        _field$props = _field$props === void 0 ? {} : _field$props;

        var _field$props$tplFrom = _field$props.tplFrom,
            tplFrom = _field$props$tplFrom === void 0 ? -1 : _field$props$tplFrom,
            _field$props$tplTo = _field$props.tplTo,
            tplTo = _field$props$tplTo === void 0 ? -1 : _field$props$tplTo,
            tabPaneProps = _objectWithoutProperties(_field$props, _excluded);

        var tabPane = _map(_slice(children, tplFrom, tplTo), function (tpl) {
          var DynamicComponent = get('DynamicComponent');
          return /*#__PURE__*/React.createElement(DynamicComponent, {
            context: context,
            template: tpl,
            onRef: _this2.onRef(tpl)
          });
        });

        return /*#__PURE__*/React.createElement(Tabs.TabPane, Object.assign({}, tabPaneProps, {
          key: field.fieldName,
          tab: field.fieldLabel
        }), tabPane);
      });

      return tabPanes;
    }
    /**
     * @param {Object} template - 模板的配置
     * @returns {Function} 将 DynamicComponent 对应组件的this设置到 this.ref 中
     */

  }, {
    key: "onRef",
    value: function onRef(template) {
      var _this3 = this;

      return function (tplRef) {
        _this3.ref[template.templateCode] = tplRef;
      };
    }
  }]);

  return DynamicTabs;
}(React.Component);

export { DynamicTabs as default };